//
//  AlertView.m
//  MeicaiCrm
//
//  Created by zhou jianbang on 2019/4/16.
//  Copyright © 2019 meicai. All rights reserved.
//

#import "AlertView.h"
#import "ViewEngine.h"
#import "Utils.h"
#import <Masonry/Masonry.h>
#define btnHeight 44
@interface AlertView ()
{
    NSArray * _buttons;
}
@property (nonatomic, strong) UILabel * titleLabel;
@property (nonatomic, strong) UILabel * msgLabel;
@property (nonatomic, strong) UIView * buttonView;

@end

@implementation AlertView

-(id)initWithDataDict:(NSDictionary *)dict andBlock:(ActionBlock)block{
    self = [super init];
    if (self) {
        [self configUIWithDict:dict];
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 2;
        self.clipsToBounds = YES;
        _block = block;
    }
    return self;
}
#pragma  mark - configSubViews
-(void)configUIWithDict:(NSDictionary *)dataDict
{
    [self addSubview:self.titleLabel];
    [self addSubview:self.msgLabel];
    [self addSubview:self.buttonView];
    
    NSDictionary * titleDict = [dataDict objectForKey:@"titleDict"];
    NSDictionary * msgDict = [dataDict objectForKey:@"msgDict"];
    NSString * direction = [dataDict objectForKey:@"direction"];
    NSArray * buttons = [dataDict objectForKey:@"buttons"];
    _buttons = buttons;
    
    [self configTitle:titleDict];
    [self configMsg:msgDict];
    [self configButtons:buttons andDirection:direction];
}
-(void)configTitle:(NSDictionary *)titleDict
{
    //标题信息
    if(titleDict){
        NSString * title = [titleDict objectForKey:@"title"];
        if (title) {
            self.titleLabel.text = title;
            self.titleLabel.textAlignment =[Utils jsonToTextAlign:[titleDict objectForKey:@"textAlign"]];
            self.titleLabel.textColor = [UIColor mcColorWithHexString:[titleDict objectForKey:@"color"]? [titleDict objectForKey:@"color"] : @"#333333"];
            NSNumber * numberOfLines = [titleDict objectForKey:@"numberOfLines"];
            self.titleLabel.numberOfLines = numberOfLines?numberOfLines.integerValue:2;
            
            NSNumber * fontSize = [titleDict objectForKey:@"fontSize"];
            NSInteger font = 16;
            if(fontSize) {
                font = fontSize.integerValue;
            }
            NSNumber * bold = [titleDict objectForKey:@"bold"];
            self.titleLabel.font = bold ? [UIFont boldSystemFontOfSize:font] : [UIFont systemFontOfSize:font];
        }
    }
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-15);
        make.top.offset(20);
        make.bottom.equalTo(self.msgLabel.mas_top).offset(-12);
    }];
}
-(void)configMsg:(NSDictionary *)msgDict
{
    //提示信息
    if(msgDict){
        NSString * title = [msgDict objectForKey:@"msg"];
        if (title) {
            self.msgLabel.text = title;
            self.msgLabel.textAlignment =[Utils jsonToTextAlign:[msgDict objectForKey:@"textAlign"]];
            self.msgLabel.textColor = [UIColor mcColorWithHexString:[msgDict objectForKey:@"color"]? [msgDict objectForKey:@"color"] : @"#333333"];
            NSNumber * numberOfLines = [msgDict objectForKey:@"numberOfLines"];
            self.msgLabel.numberOfLines = numberOfLines?numberOfLines.integerValue:2;
            
            NSNumber * fontSize = [msgDict objectForKey:@"fontSize"];
            NSInteger font = 14;
            if(fontSize) {
                font = fontSize.integerValue;
            }
            NSNumber * bold = [msgDict objectForKey:@"bold"];
            self.msgLabel.font = bold ? [UIFont boldSystemFontOfSize:font] : [UIFont systemFontOfSize:font];
        }
        [self.msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(15);
            make.right.offset(-15);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(12);
        }];
    }else{
        [self.msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(15);
            make.right.offset(-15);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(0);
        }];
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.msgLabel.mas_top).offset(-0);
        }];
    }
   
}
-(void)configButtons:(NSArray *)buttons andDirection:(NSString *)direction
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width-100*([UIScreen mainScreen].bounds.size.width/375);
    // 按钮信息
    if (buttons && buttons.count > 0) {
        NSString * currDirection = @"row";
        if(direction) currDirection = direction;
        if ([currDirection isEqualToString:@"row"]) {
            CGFloat itemWidth = width/buttons.count;
            for (NSInteger i = 0; i < buttons.count; i++) {
                NSDictionary * btnDict = (NSDictionary *)[buttons mcObjectAtIndex:i];
                if (btnDict) {
                    UIButton * button = [Utils createBtn:btnDict];
                    button.tag = i;
                    [button addTarget:self action:@selector(optionSelected:) forControlEvents:UIControlEventTouchUpInside];
                    [self.buttonView addSubview:button];
                    button.frame = CGRectMake(i*itemWidth, 0, itemWidth, btnHeight);
                    [Utils setBorderWithView:button top:NO left:NO bottom:NO right:i == buttons.count-1?NO:YES borderColor:[UIColor mcColorWithHexString:@"#F5F5F5"] borderWidth:0.4];
                }
            }
            self.buttonView.frame = CGRectMake(0, self.msgLabel.mcHeight+self.msgLabel.mcY+16, width, btnHeight);
            [Utils setBorderWithView:self.buttonView top:YES left:NO bottom:NO right:NO borderColor:[UIColor mcColorWithHexString:@"#F5F5F5"] borderWidth:0.4];
            [self.buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.msgLabel.mas_bottom).offset(16);
                make.left.and.right.and.bottom.offset(0);
                make.height.mas_equalTo(btnHeight);
            }];
        }else{
            for (NSInteger i = 0; i < buttons.count; i++) {
                NSDictionary * btnDict = (NSDictionary *)[buttons mcObjectAtIndex:i];
                if (btnDict) {
                    UIButton * button = [Utils createBtn:btnDict];
                    button.tag = i;
                    [button addTarget:self action:@selector(optionSelected:) forControlEvents:UIControlEventTouchUpInside];
                    button.frame = CGRectMake(0, i*btnHeight, width, btnHeight);
                    [Utils setBorderWithView:button top:YES left:NO bottom:NO right:NO borderColor:[UIColor mcColorWithHexString:@"#F5F5F5"] borderWidth:0.4];
                    [self.buttonView addSubview:button];
                }
                [self.buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.msgLabel.mas_bottom).offset(16);
                    make.left.and.right.and.bottom.offset(0);
                    make.height.mas_equalTo(btnHeight*buttons.count);
                }];
            }
        }
    }
}


#pragma  mark - action
-(void)optionSelected:(UIButton *)btn
{
    [ViewEngine alertHide];
    NSDictionary *btnDict = (NSDictionary *)[_buttons mcObjectAtIndex:btn.tag];
    if(btnDict){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if(self.block)self.block(@{
                                @"index":@(btn.tag),
                                @"key":[btnDict objectForKey:@"key"]
                                });
        });
    }
    
}

#pragma  mark - setter
-(UILabel *)titleLabel
{
    if(!_titleLabel){
        _titleLabel = [[UILabel alloc]init];
    }
    return _titleLabel;
}
-(UILabel *)msgLabel
{
    if(!_msgLabel){
        _msgLabel = [[UILabel alloc]init];
    }
    return _msgLabel;
}
-(UIView *)buttonView
{
    if (!_buttonView) {
        _buttonView = [[UIView alloc]init];
    }
    return _buttonView;
}
@end
